package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserVerification;
import com.yanqu.road.entity.player.UserVerificationItem;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ConcurrentHashMap;

public class UserVerificationDaoImpl extends TempDataObjectDao<UserVerification> {

    public UserVerification getUserVerification(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_verification where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserVerification data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_verification(userId,verificationItemMap,resetTime) VALUES(?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setString(i++, JSON.toJSONString(data.getVerificationItemMap()));
            call.setLong(i++, data.getResetTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserVerification data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_verification SET verificationItemMap=?,resetTime=? WHERE userId=? ";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, JSON.toJSONString(data.getVerificationItemMap()));
            call.setLong(i++, data.getResetTime());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserVerification parseData(ResultSet rs) throws SQLException {
        UserVerification data = new UserVerification();
        data.setUserId(rs.getLong("userId"));
        data.setVerificationItemMap(JSON.parseObject(rs.getString("verificationItemMap"), new TypeReference<ConcurrentHashMap<Integer, UserVerificationItem>>() {
        }));
        data.setResetTime(rs.getLong("resetTime"));
        return data;
    }

}
